﻿using ERPEF.Model;
using ERPIService;
using Microsoft.EntityFrameworkCore;
using S4Project;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ERPService
{
    //销售退货
    public class sal_SalesReturnService : sal_SalesReturnIService
    {
        private readonly MyDbContext MyDbContext;
        public sal_SalesReturnService(MyDbContext myDb)
        {
            this.MyDbContext = myDb;
        }

        public Task<int> Add(sal_sales_Return sal_Sales_Return)
        {
            MyDbContext.sal_sales_Return.Add(sal_Sales_Return);
            return MyDbContext.SaveChangesAsync();
        }

        public async Task<int> Delete(int Return_Id)
        {
            var del = MyDbContext.sal_sales_Return.Find(Return_Id);
            if (del != null)
            {
                del.IsDeleted = true;
                await MyDbContext.SaveChangesAsync();
                return 1;
            }
            return 0;
        }

        public Task<List<sal_sales_Return>> Query()
        {
            return MyDbContext.sal_sales_Return.Where(s => s.IsDeleted == false).ToListAsync();
        }


        // 根据订单编码获取销售订单
        public async Task<sal_sales_orders> GetByCode(string code)
        {
            return await MyDbContext.sal_sales_orders.FirstOrDefaultAsync(o => o.so_code == code);
        }
        // 根据订单编码获取出库订单
        public async Task<sal_sales_Out> GetByOutCode(string code)
        {
            return await MyDbContext.sal_sales_Out.FirstOrDefaultAsync(o => o.out_code == code);
        }

        // 更新销售订单信息
        public async Task Update(sal_sales_orders salesOrder)
        {
            MyDbContext.sal_sales_orders.Update(salesOrder);
            await MyDbContext.SaveChangesAsync();
        }

        // 更新销售订单信息
        public async Task UpdateOut(sal_sales_Out sal_Sales_Out)
        {
            MyDbContext.sal_sales_Out.Update(sal_Sales_Out);
            await MyDbContext.SaveChangesAsync();
        }

       
    }
}
